import os from 'os'
import di from './diskinfo'
import _ from 'lodash'
import ps from 'current-processes'

export default {
    getSystemInfo(){
        var dealTime = (seconds)=>{
            var seconds = seconds|0;
            var day = (seconds/(3600*24))|0;
            var hours = ((seconds-day*3600)/3600)|0;
            var minutes = ((seconds-day*3600*24-hours*3600)/60)|0;
            var second = seconds%60;
            (day<10)&&(day='0'+day);
            (hours<10)&&(hours='0'+hours);
            (minutes<10)&&(minutes='0'+minutes);
            (second<10)&&(second='0'+second);
            return [day,hours,minutes,second].join(':');
        };

        var dealMem = (mem)=>{
            var G = 0,
                M = 0,
                KB = 0;
            (mem>(1<<30))&&(G=(mem/(1<<30)).toFixed(2));
            (mem>(1<<20))&&(mem<(1<<30))&&(M=(mem/(1<<20)).toFixed(2));
            (mem>(1<<10))&&(mem>(1<<20))&&(KB=(mem/(1<<10)).toFixed(2));
            return G>0?G+'G':M>0?M+'M':KB>0?KB+'KB':mem+'B';
        };

        const arch = os.arch();
        console.log("cpu架构："+arch);

        const kernel = os.type();
        console.log("操作系统内核："+kernel);

        const pf = os.platform();
        console.log("平台："+pf);

        const uptime = os.uptime();
        console.log("开机时间："+dealTime(uptime));

        const hn = os.hostname();
        console.log("主机名："+hn);

        const hdir = os.homedir();
        console.log("主目录："+hdir);

        const totalMem = os.totalmem();
        const freeMem = os.freemem();
        console.log("内存大小："+dealMem(totalMem)+' 空闲内存：'+dealMem(freeMem));

        return {
            'platfrom': os.platform(),
            'arch': os.arch(),
            'hostname': os.hostname(),
            'release': os.release(),
            'type': os.type(),
            'version': os.version(),
            'cpu': os.cpus(),
            'memory': dealMem(totalMem),
        }
    },
    getCpuInfo(){
        var cpuInfo = []
        const cpus = os.cpus();
        console.log('*****cpu信息*******');
        cpus.forEach((cpu,idx,arr)=>{
            var times = cpu.times;
            console.log(`cpu${idx}：`);
            console.log(`型号：${cpu.model}`);
            console.log(`频率：${cpu.speed}MHz`);
            console.log(`使用率：${((1-times.idle/(times.idle+times.user+times.nice+times.sys+times.irq))*100).toFixed(2)}%`);
            cpu.id = idx
            cpu.usage = ((1-times.idle/(times.idle+times.user+times.nice+times.sys+times.irq))*100).toFixed(2);
            cpuInfo.push(cpu)
        });
        return cpuInfo
    },
    getNetworkInfo(){
        console.log('*****网卡信息*******');
        const networksObj = os.networkInterfaces();
        for(let nw in networksObj){
            let objArr = networksObj[nw];
            console.log(`\r\n${nw}：`);
            objArr.forEach((obj,idx,arr)=>{
                console.log(`地址：${obj.address}`);
                console.log(`掩码：${obj.netmask}`);
                console.log(`物理地址：${obj.mac}`);
                console.log(`协议族：${obj.family}`);
            });
        }
    },
    getDiskinfo(){
        var allToatal = 0
        var diskinfo = [];
        //获得所有磁盘空间
        return new Promise((resolve, reject) => {
            di.getDrives( function(err, aDrives) {
                //遍历所有磁盘信息
                for (var i = 0; i < aDrives.length; i++) {
                    //盘符号
                    var mounted = aDrives[i].mounted;
                    //总量
                    allToatal += aDrives[i].blocks
                    var total  = (aDrives[i].blocks /1024 /1024 /1024).toFixed(0) + "Gb";
                    //已使用
                    var used = (aDrives[i].used /1024 /1024 /1024).toFixed(0) + "Gb";
                    //可用
                    var available = (aDrives[i].available /1024 /1024 /1024).toFixed(0) + "Gb";
                    //使用率
                    var capacity = aDrives[i].capacity;
                    var diskItem = {'mounted': mounted,'total':total,'used':used,'available':available,'capacity':capacity}
                    diskinfo.push(diskItem)
                }
                var result = {"total":((allToatal/1024 /1024 /1024).toFixed(0) + "Gb"),diskinfo}
                resolve(result);
            })
        });
    },
    getProcessInfo(){
        return new Promise((resolve, reject) => {
            ps.get((err, processes) => {
                const sorted = _.sortBy(processes, 'cpu');
                const top10  = sorted.reverse().splice(0, 10);
                resolve(top10);
            });
        })
    }



}
